﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using Quaack.Properties;


using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Drawing;
using System.ComponentModel;

namespace Quaack
{
    public partial class BlokkeerDeBlokkeerVolger : System.Web.UI.Page
    {


        protected void page_preinit()
        {
            if (User.Identity.IsAuthenticated) Page.MasterPageFile = "~/Site-Ingelogd.Master";
            else
            {
                Page.MasterPageFile = "~/Site.Master";
                string url;
                url = "~/default.aspx";
                Response.Redirect(url);
            }
        }


        protected void Page_Load(object zender, EventArgs e)
        {

            if (User.Identity.IsAuthenticated)
            {


                int userID = Convert.ToInt32(Session["userID"]);
                int parentid = Convert.ToInt32(Session["parentmessid"]);
                int sender = Convert.ToInt32(Session["sender"]);
                Session.Add("sender", sender);
                Session.Add("parentid", parentid);
                Session.Add("userID", userID);
            }

      
        }

        protected void GridView1_RowCommand(object zender, GridViewCommandEventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection(Settings.Default.quaackdb);
            
            if (e.CommandName == "BlokeerDeblokkeer")
            {
                //int blocked = -1;
                //int followid = -1;
                //int mmessid = -1;
                // string parentmessid = GridView1.SelectedRow.Cells[5].Text.Replace("&nbsp;", "0");
                // string sender = GridView1.SelectedRow.Cells[4].Text;
                int index = int.Parse(e.CommandArgument.ToString());
                string followid = GridView1.Rows[index].Cells[0].Text.Replace("&nbsp;", "0");
                string blocked = GridView1.Rows[index].Cells[1].Text.Replace("&nbsp;", "0");
                string subject = GridView1.Rows[index].Cells[2].Text.Replace("&nbsp;", "0");
                string follower = GridView1.Rows[index].Cells[3].Text.Replace("&nbsp;", "0");
                string subjectID = GridView1.Rows[index].Cells[4].Text.Replace("&nbsp;", "0");

              //  int followidInt = Convert.ToInt32(followid);

                          
                SqlConnection SqlProfiel = new SqlConnection(Settings.Default.quaackdb);
                /*   Haal email adres op   */
                string email = "";
                SqlCommand BepaalId = new SqlCommand("Select email from profile where profileid = '" + follower + "'", SqlProfiel);
                SqlProfiel.Open();
                object sqlObject = BepaalId.ExecuteScalar();
                SqlProfiel.Close();
                email = Convert.ToString(sqlObject.ToString());
              
               // string id = Convert.ToString(email);

                /*   Haal gebruiker string van de subject ook wel degene die de follower volgt op   */

                string gebruiker = "";
                SqlCommand Gebruiker_str = new SqlCommand("Select username from profile where profileid = '" + subjectID + "'", SqlProfiel);
                SqlProfiel.Open();
                object sqlObjectGebruiker = Gebruiker_str.ExecuteScalar();
                SqlProfiel.Close();

                gebruiker = Convert.ToString(sqlObjectGebruiker.ToString());


                string title = "Leeg";
                if (blocked == "False")
                {
                    title = "Je bent geblokkeerd";
                }
                else
                {
                    title = "Je bent ge-deblokkeerd";
                }
                string body = title + " bij gebruiker " + gebruiker + ".";
                Email BlokkeerDeBlokkeerVolger = new Email();
                BlokkeerDeBlokkeerVolger.SendMail(email, title, body);

                // zet het blocke bit om in de follower tabel
                sqlConn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = sqlConn;

                if (blocked == "False")
                {
                    cmd.CommandText = "UPDATE follower SET blocked = 1 where followid = " + followid + "";
                }
                else
                {
                    cmd.CommandText = "UPDATE follower SET blocked = 0 where followid = " + followid + "";
                }
                                
                cmd.ExecuteReader();
                sqlConn.Close();

                string url;
                url = "~/BlokkeerDeBlokkeerVolger.aspx";
                Response.Redirect(url);
            }
        }
    }
}